package com.gable.report.controller;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForHospitalMaintenanceService;
import com.gable.report.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lvdouStart on 2017/9/22.
 */
@Controller
@RequestMapping("/charts/hospital/maintenance")
public class ChartsForHospitalMaintenanceController {
    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    ChartsForHospitalMaintenanceService chartsForHospitalMaintenanceService;

    @Autowired
    UserService userService;

    //医院维护报表
    @RequestMapping(value ="/department" ,method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDepartmentAssetsForMaintenance(String userId, ChartsHospitalQuery chartsHospitalQuery) {
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryDepartmentAssetsForMaintenance(hospitalId, chartsHospitalQuery);
    }


    //产品类型维护报表
    @RequestMapping(value ="/type" ,method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceTypeSubAssetsForMaintenance(String userId,ChartsHospitalQuery chartsHospitalQuery) {
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryDeviceTypeSubAssetsForMaintenance(hospitalId, chartsHospitalQuery);
    }


    //厂家维护报表
    @RequestMapping(value ="/manufacture" ,method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryManufactureAssetsForMaintenance(String userId,ChartsHospitalQuery chartsHospitalQuery) {
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryManufactureAssetsForMaintenance(hospitalId, chartsHospitalQuery);
    }


    //设备型号维护报表
    @RequestMapping(value ="/model" ,method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryModelAssetsForMaintenance(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryModelAssetsForMaintenance(hospitalId, chartsHospitalQuery);
    }


    /**
     * @Description:  "➕"号查询设备维护
     * @return
     */
    @RequestMapping(value="/device",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceInfo(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryAssets(hospitalId, chartsHospitalQuery);
    }

    /**
     * @Description:  维护概况
     * @return
     */
    @RequestMapping(value="/overview",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryOverview(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryOverview(hospitalId, chartsHospitalQuery);
    }

    /**
     * @Description:  月份维护报表
     * @return
     */
    @RequestMapping(value="/month-trend",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryForMonth(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryForMonth(hospitalId, chartsHospitalQuery);
    }

    /**
     * @Description:  巡检数据
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/queryInspectionData",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryInspectionData(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryInspectionData(hospitalId, chartsHospitalQuery);
    }

    /**
     * @Description:  强检数据
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/queryCompulsoryData",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryCompulsoryData(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryCompulsoryData(hospitalId, chartsHospitalQuery);
    }

    /**
     * @Description:  保养数据
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/queryUpkeepData",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryUpkeepData(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryUpkeepData(hospitalId, chartsHospitalQuery);
    }


    /**
     * @Author: 杨青桦
     * @Description: 查询计划设备信息
     * @Date: 2017/12/22 11:22
     * @param chartsHospitalQuery
     * @param request
     * @return
     */
    @RequestMapping(value="/plan/device",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryPlanAssets(String userId,ChartsHospitalQuery chartsHospitalQuery, HttpServletRequest request){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryPlanAssets(hospitalId, chartsHospitalQuery);
    }

    /**
     * @Author: 杨青桦
     * @Description:  查询计划记录信息
     * @Date: 2017/12/22 14:34
     * @param chartsHospitalQuery
     * @param request
     * @return
     */
    @RequestMapping(value="/plan/record",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryPlanRecords(String userId,ChartsHospitalQuery chartsHospitalQuery, HttpServletRequest request){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryPlanRecords(hospitalId, chartsHospitalQuery);
    }

    /**
     * @Author: 杨青桦
     * @Description:  查询实际维护设备信息
     * @Date: 2017/12/22 14:34
     * @param chartsHospitalQuery
     * @param request
     * @return
     */
    @RequestMapping(value="/actual/device",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryActualAssets(String userId,ChartsHospitalQuery chartsHospitalQuery, HttpServletRequest request){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalMaintenanceService.queryActualAssets(hospitalId, chartsHospitalQuery);
    }
}
